<?php
require_once("../fm/utils/conn.php");

$quantity = trim($_POST['quantity']);

$family=trim($_POST['family']);

$final=1;

$familyId= substr(time(),4,10). rand(0,89999);


 for ($i=1; $i<=$quantity; $i++){
   ${'email'.$i}=trim($_POST['email'.$i]);
   ${'password'.$i}=trim($_POST['password'.$i]);
   ${'nickname'.$i}=trim($_POST['nickname'.$i]);
 }

$db = new mysql_db();



$familySql="insert into family(title,familyId,quantity) values('".$family."','".$familyId."','".$quantity."')";
$visitorSql="insert into myvisitor(familyNum) values('".$familyId."')";
$focusSql="insert into myfocus(familyNum) values('".$familyId."')";
$focusMeSql="insert into focusme(familyNum) values('".$familyId."')";

$result=$db->query($familySql);
$visit=$db->query($visitorSql);
$focus=$db->query($focusSql);
$focusMe=$db->query($focusMeSql);
$final=$final&$result&$visit&$focus&$focusMe;

if($final){
  for ($j=1; $j<=$quantity; $j++){
        if( ${'email'.$j}==""){
        continue;
        }

        $memberSql="insert into member(nickname,password,email,familyNum) values('".${'nickname'.$j}."','". ${'password'.$j}."','".${'email'.$j}."','".$familyId."')";

        $memberResult[$j]= $db->query($memberSql);
        $final=$final&$memberResult[$j];
   }
   if($final){
      echo $db->jsonModule("success",$final);
   }else{
      echo $db->jsonModule("fail",$final);
   }
}else{
        echo $db->jsonModule("fail",$final);
     }

?>